package vn.sms.lazylove;

import vn.sms.lazylove.database.DataStore;
import vn.sms.lazylove.object.SchedulerSMSItem;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Binder;
import android.os.IBinder;
import android.os.SystemClock;
import android.telephony.SmsManager;
import android.widget.Toast;

public class LazyLoveService extends Service {

	private NotificationManager mNM;
	public static String mTitle;
	DataStore mDataStore;
	SchedulerSMSItem smsItem;
	String phonenumber, messagebody, week, time;

	public class LazyLoveBinder extends Binder {
		public LazyLoveService getService() {
			return LazyLoveService.this;
		}
	}

	@Override
	public void onCreate() {
		mDataStore = DataStore.getInstance();
		mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		showNotification();
		startactivity();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
	}

	@Override
	public void onDestroy() {
		mNM.cancel(R.string.lazylove_service_started);
	}

	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}

	private final IBinder mBinder = new LazyLoveBinder();

	private void showNotification() {
		String playingSong = "Lazy love sms";
		CharSequence text = String.valueOf(playingSong);
		Notification notification = new Notification(
				R.drawable.ic_notification, text, System.currentTimeMillis());
		notification.flags |= Notification.FLAG_ONGOING_EVENT;
		Intent _i = new Intent(getApplicationContext(), CreateSmsActivity.class)
				.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
						| Intent.FLAG_ACTIVITY_SINGLE_TOP);
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0, _i, 0);
		notification.setLatestEventInfo(this, getString(R.string.app_name),
				text, contentIntent);
		mNM.notify(R.string.lazylove_service_started, notification);
	}

	public void startactivity() {
		// get all sms in data
		// List<SchedulerSMSItem> list = mDataStore.getListSMSScheduler();
		// if (list.size() > 0) {
		// smsItem = list.get(0);
		// if (smsItem.status == 1) {
		// week = smsItem.week;
		// time = smsItem.time;
		// phonenumber = smsItem.phone;
		// messagebody = smsItem.message;
		// }
		// }

		Intent intent = new Intent(LazyLoveService.this, RepeatingAlarm.class);
		PendingIntent sender = PendingIntent.getBroadcast(LazyLoveService.this,
				0, intent, 0);

		// We want the alarm to go off 30 seconds from now.
		long firstTime = SystemClock.elapsedRealtime();
		firstTime += 15 * 1000;

		// Schedule the alarm!
		AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
		am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime,
				15 * 1000, sender);
	}

	// ---sends an SMS message to another device---
	private void sendSMS(String phoneNumber, String message) {
		String SENT = "SMS_SENT";
		String DELIVERED = "SMS_DELIVERED";

		PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
				SENT), 0);

		PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
				new Intent(DELIVERED), 0);

		// ---when the SMS has been sent---
		registerReceiver(new BroadcastReceiver() {
			@Override
			public void onReceive(Context arg0, Intent arg1) {
				switch (getResultCode()) {
				case Activity.RESULT_OK:
					Toast.makeText(getBaseContext(), "SMS sent",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
					Toast.makeText(getBaseContext(), "Generic failure",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_NO_SERVICE:
					Toast.makeText(getBaseContext(), "No service",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_NULL_PDU:
					Toast.makeText(getBaseContext(), "Null PDU",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_RADIO_OFF:
					Toast.makeText(getBaseContext(), "Radio off",
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
		}, new IntentFilter(SENT));

		// ---when the SMS has been delivered---
		registerReceiver(new BroadcastReceiver() {
			@Override
			public void onReceive(Context arg0, Intent arg1) {
				switch (getResultCode()) {
				case Activity.RESULT_OK:
					Toast.makeText(getBaseContext(), "SMS delivered",
							Toast.LENGTH_SHORT).show();
					break;
				case Activity.RESULT_CANCELED:
					Toast.makeText(getBaseContext(), "SMS not delivered",
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
		}, new IntentFilter(DELIVERED));

		SmsManager sms = SmsManager.getDefault();
		sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
	}

	public class RepeatingAlarm extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			Toast.makeText(context, "repeating_received", Toast.LENGTH_SHORT)
					.show();
		}

	}

}
